package com.dotflat.pay.controller;

import com.starinside.wp.domain.WxUser;
import com.starinside.wp.foundation.model.StatusResponse;
import com.starinside.wp.foundation.vo.BindedBabyVO;
import com.starinside.wp.utils.RemoteCallUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

import static com.starinside.wp.foundation.constant.RequestConstant.TICKET;
import static org.springframework.web.bind.annotation.RequestMethod.GET;

@Slf4j
@RestController
@RequestMapping("/v2/pay/alipay")
@Api(tags = "缴费 - 支付宝支付")
public class PayAliController  {

    @Value("${server.crm.url}")
    private String crmServerURL;

    @RequestMapping(value = "/prepare",method = RequestMethod.POST)
    @ApiOperation("拉起支付")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "resourceType", required = true, value = "资源类型，0充值，1收费单", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "resourceId", value = "通常是收费单id", dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "openId", value = "公众号openid，公众号/小程序支付逻辑请填写", dataType = "string")
    })
    public Object getAlipayOrder(HttpServletRequest req,
                                 @RequestHeader(TICKET) String ticket,
                                 @RequestParam Integer resourceType,
                                 String openId,
                                 @RequestParam String resourceId,
                                 @RequestParam(defaultValue = "0") int useDeposit){

        WxUser user = (WxUser) req.getSession().getAttribute("loginUser");

        BindedBabyVO defaultBaby = user.getDefaultBaby();

//        if (defaultBaby == null || StringUtils.isBlank(defaultBaby.getCrmClienteleId())){
//            return StatusResponse.error(ErrorCode.CHILDREN_STATUS_ERROR, "宝宝在园状态不对");
//        }

        Map p = new HashMap<>();

        if (resourceType != null){
            p.put("resourceType", resourceType);
        }
        if (StringUtils.isNotEmpty(resourceId)){
            p.put("resourceId", resourceId);
        }
        if (StringUtils.isNotEmpty(openId)){
            p.put("openId", openId);
        }

        p.put("useDeposit", useDeposit);

        p.put("sourceId", user.getDefaultBabyId());
        p.put("clienteleId", defaultBaby.getCrmClienteleId());

        HashMap result = RemoteCallUtil.call(crmServerURL + "/api/other_system/pay/alipay/prepare", p, null, HashMap.class, "获取订单失败");

        return StatusResponse.success(result);
    }

    @RequestMapping(value = "/order/check", method = GET )
    public Object wxOrderCheck(@RequestHeader(TICKET) String ticket,
                               @RequestParam("resourceType") Integer resourceType,
                               @RequestParam("outTradeNo") String outTradeNo) {
        //先添加CRM客户
        HashMap result = RemoteCallUtil.call(crmServerURL + "/api/other_system/pay/alipay/check", null, ticket, HashMap.class, "获取订单失败");

        return StatusResponse.success();
    }
}
